package com.sann.eduservice.service.impl;

import com.sann.common.utils.ConstantUtil;
import com.sann.common.utils.ResultCode;
import com.sann.eduservice.entity.course.Course;
import com.sann.eduservice.entity.course.CourseDescription;
import com.sann.eduservice.entity.course.vo.CourseInfoVo;
import com.sann.eduservice.entity.course.vo.CoursePublishVO;
import com.sann.eduservice.mapper.CourseMapper;
import com.sann.eduservice.service.ChapterService;
import com.sann.eduservice.service.CourseDescriptionService;
import com.sann.eduservice.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sann.eduservice.service.VideoService;
import com.sann.servicebase.handler.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zjw
 * @since 2021-04-17
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;


    // 添加课程
    @Override
    public String insert(CourseInfoVo courseInfoVo) {
        // 1、添加课程
        Course course = new Course();
        course.setStatus(ConstantUtil.DRAFT);
        BeanUtils.copyProperties(courseInfoVo, course);
        boolean saveCourse = this.save(course);

        if (!saveCourse) {
            throw new GuliException(ResultCode.ERROR, "添加课程信息失败");
        }
        // TODO baseMapper.insert如何做到将新插入的记录ID保存到实体类的？
        //获取添加课程之后的课程ID
        String courseId = course.getId();

        // 2、添加课程简介
        CourseDescription description = new CourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        description.setId(courseId);
        boolean saveDescription = courseDescriptionService.save(description);
        if (!saveDescription) {
            throw new GuliException(ResultCode.ERROR, "保存课程简介失败");
        }
        return courseId;
    }

    // 根据课程ID查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfoVoById(String courseId) {
        // 查询课程
        Course course = this.baseMapper.selectById(courseId);

        // 查询课程描述
        CourseDescription courseDescription = this.courseDescriptionService.getById(courseId);

        // 创建返回的VO对象
        CourseInfoVo infoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course, infoVo);
        infoVo.setDescription(courseDescription.getDescription());

        return infoVo;
    }

    // 修改课程信息
    @Override
    public int updateCourseInfo(CourseInfoVo courseInfoVo) {
        // 修改课程信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
        boolean bol = this.updateById(course);
        if (!bol) {
            throw new GuliException(ResultCode.ERROR, "修改课程信息失败");
        }

        // 修改简介信息
        CourseDescription description = new CourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        boolean bol2 = this.courseDescriptionService.updateById(description);

        if (!bol2) {
            throw new GuliException(ResultCode.ERROR, "修改课程简介失败");
        }

        return ResultCode.RETURN_SUCCESS_NUM;
    }

    //根据课程courseId获取发布课程信息
    @Override
    public CoursePublishVO getCoursePublishById(String courseId) {
        CoursePublishVO coursePublishVO = baseMapper.getCoursePublishById(courseId);
        return coursePublishVO;
    }

    // 执行发布课程
    @Override
    public boolean publishCourseByCourseId(String courseId) {
        Course course = new Course();
        course.setId(courseId);
        course.setStatus(Course.COURSE_NORMAL);
        Integer counts = this.baseMapper.updateById(course);
        return null != counts && counts > 0;
    }


    @Override
    public void delete(String courseId) {
        //1. 根据课程id删除小节
        this.videoService.removeByCourseId(courseId);

        //2. 根据课程id删除章节
        this.chapterService.removeByCourseId(courseId);

        //3. 根据课程id删除描述,描述表主键ID是依赖于课程表主键ID，可以直接调用CourseDescriptionService方法
        this.courseDescriptionService.removeById(courseId);

        //4. 根据课程id删除课程本身
        int count = this.baseMapper.deleteById(courseId);
        if (count == 0) {
            throw new GuliException(ResultCode.ERROR, "删除课程失败");
        }
    }


}
